package com.frikinet.tools.gui;

import java.util.HashMap;
import java.util.Map;

import javax.swing.ButtonGroup;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

import com.frikinet.tools.i18n.FI18N;
import com.frikinet.tools.i18n.Translable;

public class FRadioButtonMenuItem extends JRadioButtonMenuItem implements Translable, Indexable
{
	private static final long serialVersionUID = 1L;
	private static Map<String,ButtonGroup> groups = new HashMap<String,ButtonGroup>();

	private FI18N i18n = null;
	private String id = "";

	public FI18N getI18n()
	{
		return i18n;
	}

	public void setI18N(FI18N i18n)
	{
		this.i18n = i18n;
	}
	
	public void translate()
	{
		String text = getText();
		if(i18n != null)
			super.setText(parseName(i18n.translate(text)));
		else
			super.setText(parseName(text));
	}
	
	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	private String parseName(String text)
	{
		String stripString = text.replaceAll("_", "");
		int index = text.indexOf('_');
		if(index >= 0)
			setMnemonic(text.charAt(index+1));
		return stripString;
	}
	
	public void setAcceleratorString(String keys)
	{
		setAccelerator(KeyStroke.getKeyStroke(keys));
	}
	
	public void setGroup(String group)
	{
		ButtonGroup bg = null;
		if(groups.containsKey(group))
		{
			bg = groups.get(group);
		}
		else
		{
			bg = new ButtonGroup();
			groups.put(group, bg);
		}
		bg.add(this);
	}
}
